/**
 * @author Qi
 */
function ContactManager(){
	this.eventHandlers = null;
	this.dataManager = null;
	this.currentTab = "xx";
	this.currentContactIndex = -1;
	this.initTimer = -1;
	
	this.init = function(){
		contactManager.eventHandlers = new EventHandlers();
		contactManager.eventHandlers.init();
		contactManager.dataManager = new DataManager();
		this.initTimer = setTimeout("contactManager.initStorage()",500);
	};
	this.initStorage = function(){
		if(dojox.storage.manager.isInitialized()){
			clearTimeout(this.initTimer);
			contactManager.dataManager.init();
			contactManager.displayContactList();
			dojo.byId("divInitializing").style.display = "none";
			dojo.byId("content").style.display = "block";
			this.initTimer = null;
		}else{
			this.initTimer = setTimeout("contactManager.initStorage()",500);
		}
	};
	this.displayContactList = function(){
		var contacts = this.dataManager.listContacts(this.currentTab);
		
		var html = "";
		var alt = false;
		for(var i = 0;i < contacts.length;i++){
			html += "<div indexNum=\"" + contacts[i].arrayIndex +"\" ";
			html += "onmouseover=\"contactManager.eventHandlers.clOver(this);\" ";
			html += "onmouseout=\"contactManager.eventHandlers.clOut(this);\" ";
			html += "onclick=\"contactManager.doEditContact(this.getAttribute('indexNum'));\" ";
			if(alt){
				html += "class=\"cssContactListAlternate\" altRow=\"true\">";
				alt = false;
			}else{
				html += "class=\"cssContactListNormal\" altRow=\"false\">";
				alt = true;
			}
			html += contacts[i].lastName + ", " + contacts[i].firstName;
			html += "</div>";
		}
		dojo.byId("contactList").innerHTML = html;
	};
	this.doEditContact = function(inIndex){
		this.currentContactIndex = inIndex;
		var contact = this.dataManager.getContact(inIndex);
		contact.populateScreen();
	};
	this.doNewContact = function(){
		if(this.initTimer == null){
			if(confirm("Create New Contact\n\nYou will lost any unsaved changes.  Are you sure?")){
				document.forms[0].reset();
				this.currentContactIndex = -1;
			}
		}
	};
	this.doSaveContact = function(){
		if(this.initTimer == null){
			if(dojo.byId("firstName").value == "" || dojo.byId("lastName").value == ""){
				alert("First Name and Last Name are required fields");
				return false;
			}
			var contact = new Contact();
			contact.arrayIndex = this.currentContactIndex;
			contact.populateContact();
			this.dataManager.saveContact(contact,this.currentContactIndex);
			
			this.displayContactList();
			
			document.forms[0].reset();
			this.currentContactIndex = -1;
		}
	};
	this.doDeleteContact = function(){
		if(this.initTimer == null){
			if(this.currentContactIndex != -1 &&
		   confirm("Are you sure you want to delete this contact?")){
		   	this.dataManager.deleteContact(this.currentContactIndex);
			this.displayContactList();
			document.forms[0].reset();
			this.currentContactIndex = -1;
		   }
		}
	};
	this.doClearContacts = function(){
		if(this.initTimer == null){
			if(confirm("That will Permanently delete all contacts from persistent storage\n\nAre you sure?")){
				this.dataManager.clearContacts();
				this.displayContactList();
				document.forms[0].reset();
				this.currentContactIndex = -1;
			}
		}
	};
	this.doExit = function(){
		if(this.initTimer == null){
			if(confirm("Exit Contact Manager\n\nAre you sure?")){
				window.location = "goodbye.html";
			}
		}
	};
	
}
